/** Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.*/
package com.truba.ohostouchgallery.slice;

import ohos.agp.window.dialog.ToastDialog;
import ru.truba.touchgrallery.GalleryWidget.BasePagerAdapter;
import ru.truba.touchgrallery.GalleryWidget.GalleryViewPager;
import ru.truba.touchgrallery.GalleryWidget.UrlPagerAdapter;
import com.truba.ohostouchgallery.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GalleryUrlActivitySlice extends AbilitySlice {
    private GalleryViewPager mViewPager;
    ToastDialog dialog;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_main);
        String[] urls = {
                "https://cs407831.userapi.com/v407831207/18f6/jBaVZFDhXRA.jpg",
                "https://img1.baidu.com/it/u=2658872482,510079581&fm=26&fmt=auto&gp=0.jpg",
                "https://cs407831.userapi.com/v407831207/1906/oxoP6URjFtA.jpg",
                "https://cs407831.userapi.com/v407831207/190e/2Sz9A774hUc.jpg",
                "https://cs407831.userapi.com/v407831207/1916/Ua52RjnKqjk.jpg",
                "https://cs407831.userapi.com/v407831207/191e/QEQE83Ok0lQ.jpg"
        };
        List<String> items = new ArrayList<String>();
        Collections.addAll(items, urls);

        UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(getContext(), items);
        pagerAdapter.setOnItemChangeListener(new BasePagerAdapter.OnItemChangeListener()
        {
            @Override
            public void onItemChange(int currentPosition)
            {
                getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if (dialog==null) {
                            dialog = new ToastDialog(getContext());
                        }
                        dialog.setText("Current item is " + currentPosition).show();
                    }
                });

            }
        });

        mViewPager = (GalleryViewPager)findComponentById(ResourceTable.Id_viewer);
        mViewPager.setPageCacheSize(3);
        mViewPager.setProvider(pagerAdapter);
        mViewPager.addPageChangedListener( pagerAdapter.new pagechangeList(mViewPager));
    }


}
